package com.huluobo.a2109monthdemo.home

import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.ImageView
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.GridLayoutManager
import com.huluobo.a2109monthdemo.R
import com.huluobo.a2109monthdemo.Utils.ImageUtils
import com.huluobo.a2109monthdemo.base.BaseMVVMFragment
import com.huluobo.a2109monthdemo.databinding.FragmentHomeBinding
import com.huluobo.a2109monthdemo.detail.DetailActivity
import com.huluobo.a2109monthdemo.net.Const
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.BannerConfig
import com.youth.banner.loader.ImageLoader

class HomeFragment : BaseMVVMFragment<FragmentHomeBinding>() {
    private lateinit var viewModel: HomeViewModel

    private val bannerUrls = mutableListOf<String>()

    private val navList = mutableListOf<HomeNavBean>()

    private lateinit var navAdapter: HomeNavAdapter

    private val goodsList = mutableListOf<HomeGoodsListBean>()
    private lateinit var goodsListAdapter: HomeGoodsListAdapter

    private var currentPage = 1
    override fun getLayoutId(): Int = R.layout.fragment_home

    override fun initData() {
        viewModel = ViewModelProvider(requireActivity())[HomeViewModel::class.java]

        viewModel.getHomeBanner()
        viewModel.getHomeNav()
        viewModel.getHomeGoodsList(0, currentPage, 10)

        viewModel.homeBannerSuccess.observe(this) {
            if (it.code == 200) {
                bannerUrls.clear()
                it.data.forEach {
                    bannerUrls.add(it.imagePath)
                }
                dataBinding.homeBanner.setImages(bannerUrls)
                dataBinding.homeBanner.start()
            }

        }
        viewModel.homeBannerFailed.observe(this) {

        }

        viewModel.homeNavSuccess.observe(this) {
            if (it.code == 200) {
                navList.clear()
                navList.addAll(it.data[0].data!!)
                navAdapter.notifyDataSetChanged()
            }
        }

        viewModel.homeNavFailed.observe(this) {

        }

        viewModel.homeGoodsListSuccess.observe(this) {
            if (it.code == 200) {
                if (currentPage == 1) {
                    goodsList.clear()
                }
                dataBinding.homeRefresh.finishRefresh()
                dataBinding.homeRefresh.finishLoadMore()
                goodsList.addAll(it.data)
                goodsListAdapter.notifyDataSetChanged()
            }
        }

        viewModel.homeGoodsListFailed.observe(this) {

        }

    }

    override fun initView() {
        //初始化banner
        dataBinding.homeBanner.setBannerStyle(BannerConfig.NUM_INDICATOR)
        dataBinding.homeBanner.setImageLoader(object : ImageLoader() {
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                if (context != null && imageView != null) {
                    ImageUtils.loadImage(context, path.toString(), imageView)
                }
            }
        })
        dataBinding.homeBanner.setDelayTime(1500)
        dataBinding.homeBanner.setIndicatorGravity(BannerConfig.CENTER)

        //初始化导航
        dataBinding.homeNavRv.layoutManager = GridLayoutManager(context, 5)
        navAdapter = HomeNavAdapter(navList)
        dataBinding.homeNavRv.adapter = navAdapter

        //初始化首页商品列表
        dataBinding.homeGoodsRv.layoutManager = GridLayoutManager(context, 2)
        goodsListAdapter = HomeGoodsListAdapter(goodsList)
        dataBinding.homeGoodsRv.adapter = goodsListAdapter

        goodsListAdapter.setOnItemClickListener { _, _, position ->
            val intent = Intent(requireContext(), DetailActivity::class.java)
            intent.putExtra(Const.PARAM_GOODS_ID, goodsList[position].id)
            startActivity(intent)
        }

        //初始化删啦刷新和下拉加载
        dataBinding.homeRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                if (refreshLayout.isLoading) {
                    refreshLayout.finishLoadMore()
                }
                currentPage = 1
                viewModel.getHomeGoodsList(0, currentPage, 10)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (refreshLayout.isRefreshing) {
                    refreshLayout.finishRefresh()
                }
                currentPage += 1
                viewModel.getHomeGoodsList(0, currentPage, 10)
            }

        })
    }
}